﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using S7.Net;


namespace WindowsFormsApp1
{
    public partial class Form2 : Form
    {
        public static Plc PLC_Type;
        public static Form1 f1 = new Form1();
        public Form2()
        {
            InitializeComponent();
        }

        private void buttonNext_Click(object sender, EventArgs e)
        {
            this.Hide();
            f1.Show();
        }

        private void ConnectButton_Click(object sender, EventArgs e)
        {
            //创建连接对象
            switch(PLCType.Text)
            {
                case "S7200": PLC_Type = new Plc(CpuType.S7200, IPAdrees.Text, Convert.ToInt16(rackN.Value), Convert.ToInt16(Solt.Value)); break;
                case "S7300": PLC_Type = new Plc(CpuType.S7300, IPAdrees.Text, Convert.ToInt16(rackN.Value), Convert.ToInt16(Solt.Value)); break;
                case "S7400": PLC_Type = new Plc(CpuType.S7400, IPAdrees.Text, Convert.ToInt16(rackN.Value), Convert.ToInt16(Solt.Value)); break;
                case "S71200": PLC_Type = new Plc(CpuType.S71200, IPAdrees.Text, Convert.ToInt16(rackN.Value), Convert.ToInt16(Solt.Value)); break;
                case "S71500": PLC_Type = new Plc(CpuType.S71500, IPAdrees.Text, Convert.ToInt16(rackN.Value), Convert.ToInt16(Solt.Value)); break;
                default:break;
            }
            //开始连接并返回故障码
            switch(PLC_Type.Open())
            {
                case ErrorCode.ConnectionError:ConnecteState.Text = "连接失败";break;
                case ErrorCode.IPAddressNotAvailable: ConnecteState.Text = "IP地址无效"; break;
                case ErrorCode.NoError: ConnecteState.Text = "连接成功"; break;
                case ErrorCode.SendData: ConnecteState.Text = "发送数据中"; break;
                case ErrorCode.ReadData: ConnecteState.Text = "读取数据中"; break;
                default:break;
            }
        }

        private void DisConnectButton_Click(object sender, EventArgs e)
        {
            if (PLC_Type != null)
            {
                PLC_Type.Close();
                ConnecteState.Text = "";
            }      
        }

        private void Readbutton_Click(object sender, EventArgs e)
        {
            double MyPlcData=0.0;
            try
            {
                switch (DateType.Text)
                {
                    case "Bit": MyPlcData = Convert.ToDouble((Boolean)(PLC_Type.Read("DB" + DBText.Value + "." + "DBX" + StartText.Value+"."+BitSelect.Value))); break;
                    case "Byte": MyPlcData = Convert.ToDouble((Byte)(PLC_Type.Read("DB" + DBText.Value + "." + "DBB" + StartText.Value))); break;
                    case "Word": MyPlcData = Convert.ToDouble((UInt16)(PLC_Type.Read("DB" + DBText.Value + "." + "DBW" + StartText.Value))); break;
                    case "DWord": MyPlcData = Convert.ToDouble((UInt32)(PLC_Type.Read("DB" + DBText.Value + "." + "DBD" + StartText.Value))); break;
                    case "Int": MyPlcData = BitConverter.ToInt16(BitConverter.GetBytes((UInt16)(PLC_Type.Read("DB" + DBText.Value + "." + "DBW" + StartText.Value))),0); break;
                    case "DInt": MyPlcData = BitConverter.ToInt32(BitConverter.GetBytes((UInt32)(PLC_Type.Read("DB" + DBText.Value + "." + "DBD" + StartText.Value))),0); break;
                    case "Real": MyPlcData = BitConverter.ToSingle(BitConverter.GetBytes((UInt32)PLC_Type.Read("DB" + DBText.Value + "." + "DBD" + StartText.Value)),0); break;
                    default: break;
                }
            }
            catch(Exception e1)
            {

            }
            MessageBox.Show(MyPlcData.ToString());
        }
        private void Writebutton_Click(object sender, EventArgs e)
        {
            ErrorCode errorCode=0;
            string MyData = "";
            try
            {
                switch(DateType.Text)
                {
                    case "Bit": errorCode=PLC_Type.Write("DB" + DBText.Value + "." + "DBX" + StartText.Value+"."+BitSelect.Value, Convert.ToBoolean(Convert.ToInt16(WriteText.Text))); break;
                    case "Byte": Byte[] bytes=new Byte[1];bytes[0] = Convert.ToByte(WriteText.Text);errorCode = PLC_Type.WriteBytes(DataType.DataBlock, (int)DBText.Value, (int)StartText.Value,bytes); break;
                    case "Word": errorCode = PLC_Type.Write("DB" + DBText.Value + "." + "DBW" + StartText.Value, Convert.ToUInt16(WriteText.Text)); break;
                    case "DWord": errorCode = PLC_Type.Write("DB" + DBText.Value + "." + "DBD" + StartText.Value, Convert.ToUInt32(WriteText.Text)); break;
                    case "Int": errorCode = PLC_Type.Write("DB" + DBText.Value + "." + "DBW" + StartText.Value, (Int16)Convert.ToSingle(WriteText.Text)); break;
                    case "DInt": errorCode = PLC_Type.Write("DB" + DBText.Value + "." + "DBD" + StartText.Value, (Int32)Convert.ToSingle(WriteText.Text)); break;
                    case "Real": errorCode = PLC_Type.Write("DB" + DBText.Value + "." + "DBD" + StartText.Value, BitConverter.ToUInt32(BitConverter.GetBytes(Convert.ToSingle(WriteText.Text)),0)); break;
                    default:break;
                }
                switch(errorCode)
                {
                    case ErrorCode.ConnectionError: MyData = "连接失败"; break;
                    case ErrorCode.IPAddressNotAvailable: MyData = "IP地址无效"; break;
                    case ErrorCode.NoError: MyData = "连接成功"; break;
                    case ErrorCode.SendData: MyData = "发送数据中"; break;
                    case ErrorCode.WriteData:MyData = "写入数据中";break;
                    case ErrorCode.ReadData:MyData = "读取数据中";break;
                    case ErrorCode.WrongVarFormat: MyData = "错误的值格式"; break;
                    default: break;
                }
            }
            catch (Exception e1)
            {

            }
            MessageBox.Show(MyData);
        }
        private void IPAdrees_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)8) && (e.KeyChar != Convert.ToChar(".")) )
                e.Handled = true;//e.handled=true即可,无效化刚刚输入的键值      
        }

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(char.IsNumber(e.KeyChar)) && (e.KeyChar != (char)8) && (e.KeyChar != Convert.ToChar("."))&&(e.KeyChar != Convert.ToChar("-")) )
                e.Handled = true;//e.handled=true即可,无效化刚刚输入的键值  
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DateType.Text == "Bit")
                BitSelect.Enabled = true;
            else
                BitSelect.Enabled = false; 
        }

    }
}
